<template>
  <el-container class="dispatch-view panel-block">
    <el-header class="sa-header">
      <el-tabs class="sa-tabs" v-model="dispatchType" @tab-change="onChangeTab">
        <el-tab-pane label="物流快递" name="express"></el-tab-pane>
        <el-tab-pane label="自动发货" name="autosend"></el-tab-pane>
      </el-tabs>
      <div class="sa-title sa-flex sa-row-between">
        <div class="label sa-flex">发货模板</div>
        <div>
          <el-button class="sa-button-refresh" icon="RefreshRight" @click="getData()"></el-button>
          <el-button v-auth="'shop.admin.dispatch.dispatch.add'" icon="Plus" type="primary" @click="onAdd">添加</el-button>
        </div>
      </div>
    </el-header>
    <el-main class="sa-p-0" v-loading="loading">
      <div v-if="dispatchType == 'express'">
        <sa-empty v-if="collapse.data.length == 0" />
        <template v-else>
          <div v-for="c in collapse.data" :key="c" class="collapse sa-m-b-16">
            <div class="sa-flex sa-row-between collapse-list-header sa-m-b-2">
              <div class="sa-flex name">
                <div class="sa-m-r-8">#{{ c.id }}</div>
                <div>{{ c.name }}</div>
              </div>
              <div class="sa-flex collapse-list-box">
                <div class="time">最后编辑时间:{{ c.update_time }}</div>
                <el-button v-auth="'shop.admin.dispatch.dispatch.detail'" type="primary" class="is-link"
                  @click.stop="onEdit(c.id, 'edit')">编辑</el-button>
                <!-- TODO:  功能待讨论 -->
                <el-button v-auth="'shop.admin.dispatch.dispatch.detail'" type="info" class="is-link copy-btn"
                  @click.stop="onEdit(c.id, 'copy')">复制</el-button>
                <el-popconfirm width="fit-content" confirm-button-text="确认" cancel-button-text="取消" title="确认删除这条记录?"
                  @confirm="onDelete(c.id)">
                  <template #reference>
                    <el-button v-auth="'shop.admin.dispatch.dispatch.delete'" type="danger" class="is-link" @click.stop>
                      删除
                    </el-button>
                  </template>
                </el-popconfirm>
              </div>
            </div>

            <div v-for="(e, index) in c.express" :key="e">
              <div class="sa-flex sa-row-between collapse-item-header" v-if="index == 0">
                <div class="area sa-m-r-60">可配送区域</div>
                <div class="sa-flex">
                  <div class="box">
                    {{ e.type == 'weight' ? '首重(kg)' : '首件' }}
                  </div>
                  <div class="box">运费(元)</div>
                  <div class="box">
                    {{ e.type == 'weight' ? '续重(kg)' : '续件' }}
                  </div>
                  <div class="box">续费(元)</div>
                </div>
              </div>
              <div class="sa-flex sa-row-between district">
                <!-- TODO:省市区接口 列表展示 -->
                <div class="area sa-m-r-60">{{ e.district_text || '-' }}</div>
                <div class="sa-flex">
                  <div class="box">{{ e.first_num }}</div>
                  <div class="box">{{ e.first_price }}</div>
                  <div class="box">{{ e.additional_num }}</div>
                  <div class="box">{{ e.additional_price }}</div>
                </div>
              </div>
            </div>
          </div>
        </template>
      </div>
      <el-table v-if="dispatchType == 'autosend'" class="sa-table" :data="collapse.data">
        <el-table-column prop="id" label="ID" min-width="90">
        </el-table-column>
        <el-table-column label="模板名称" min-width="140">
          <template #default="scope">
            <div class="sa-line-1">
              {{ scope.row.name }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="自动发货内容" min-width="400">
          <template #default="scope">
            <div>
              <div v-if="scope.row.autosend?.type == 'text'">
                {{ scope.row.autosend?.content }}</div>
              <div v-else>
                <template v-for="(item, index) in scope.row.autosend?.content">
                  {{ item.title }}:{{ item.content }}
                  <span v-if="index != scope.row.autosend?.content.length - 1" class="sa-m-r-4">;</span>
                </template>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="update_time" label="最后编辑时间" min-width="172">
        </el-table-column>
        <el-table-column label="操作" min-width="180">
          <template #default="scope">
            <el-button v-auth="'shop.admin.dispatch.dispatch.detail'" type="primary" class="is-link"
              @click="onEdit(scope.row.id, 'edit')">编辑</el-button>

            <el-button v-auth="'shop.admin.dispatch.dispatch.detail'" type="info" link
              @click="onEdit(scope.row.id, 'copy')">复制</el-button>

            <el-popconfirm width="fit-content" confirm-button-text="确认" cancel-button-text="取消" title="确认删除这条记录?"
              @confirm="onDelete(scope.row.id)">
              <template #reference>
                <el-button v-auth="'shop.admin.dispatch.dispatch.edit'" type="danger" link> 删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <sa-view-bar>
      <template #right>
        <sa-pagination :pageData="pageData" @updateFn="getData" />
      </template>
    </sa-view-bar>
  </el-container>
</template>
<script>
  export default {
    name: 'shop.admin.dispatch.dispatch',
  };
</script>
<script setup>
  import { onMounted, reactive, ref } from 'vue';
  import { api } from './dispatch.service';
  import { useModal } from '@/sheep/hooks';
  import { usePagination } from '@/sheep/hooks';
  import DispatchEdit from './edit.vue';
  const { pageData } = usePagination();
  const dispatchType = ref('express');
  const collapse = reactive({
    data: [],
    order: '',
    sort: '',
  });
  const loading = ref(true);
  // 获取数据
  async function getData() {
    loading.value = true;
    const { error, data } = await api.list({
      order: collapse.order,
      sort: collapse.sort,
      page: pageData.page,
      list_rows: pageData.list_rows,
      type: dispatchType.value
    });
    if (error === 0) {
      collapse.data = data.data;
      pageData.page = data.current_page;
      pageData.list_rows = data.per_page;
      pageData.total = data.total;
    }
    loading.value = false;
  }

  function onChangeTab() {
    pageData.page = 1
    getData()
  }

  function onAdd() {
    useModal(
      DispatchEdit,
      { title: '添加模板', type: 'add', dispatchType: dispatchType.value, },
      {
        confirm: () => {
          getData();
        },
      },
    );
  }
  function onEdit(id, type) {
    useModal(
      DispatchEdit,
      {
        title: type == 'edit' ? '编辑' : '复制',
        type: type,
        id,
        dispatchType: dispatchType.value,
      },
      {
        confirm: () => {
          getData();
        },
      },
    );
  }
  async function onDelete(id) {
    await api.delete(id, {
      type: dispatchType.value
    });
    getData();
  }
  onMounted(() => {
    getData();
  });
</script>
<style lang="scss" scoped>
  .collapse {
    min-width: 100%;
    display: inline-block;
    border-radius: 4px;
    border: 1px solid var(--sa-space);
  }

  .collapse-list-header {
    font-size: 12px;
    line-height: 14px;
    color: var(--sa-subtitle);
    height: 40px;
    padding: 0 16px 0 24px;
    flex: 1 0 90%;
    order: 1;
    border-radius: 4px;
    background: var(--t-bg-disabled);
    .name {
      min-width: 748px;
      font-size: 14px;
      font-weight: 600;
      color: var(--sa-font);
    }
    .time {
      font-size: 14px;
      width: 240px;
      font-weight: 500;
      color: var(--sa-font);
    }
  }
  .collapse-item-header {
    padding: 10px 0 10px 16px;
    font-size: 14px;
    color: var(--sa-font);
    height: 40px;
    border-bottom: 1px solid var(--sa-space);
    box-shadow: 0 2px 6px rgb(140 140 140 / 12%);
    .area {
      min-width: 748px;
    }
    .box {
      min-width: 88px;
      padding: 0 18px;
      text-align: center;
    }
  }
  .district {
    min-height: 56px;
    padding: 10px 0 10px 16px;
    font-size: 14px;
    color: var(--sa-font);
    border-bottom: 1px solid var(--sa-space);
    .area {
      min-width: 808px;
      font-size: 12px;
    }
    .box {
      min-width: 88px;
      padding: 0 18px;
      text-align: center;
    }
  }
  .copy-btn {
    --el-button-text-color: var(--sa-font);
  }
  :deep() {
    .el-button > span {
      font-weight: 500;
    }
  }
</style>
